<?php

namespace app\admin\controller;

use app\models\MpModel;

class Mp extends Common
{
    /**
     * 媒体列表
     * User: wufeng
     * Date: 2020/04/15.
     *
     * @throws \think\exception\DbException
     *
     * @return mixed
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $param = $this->request->param();
            $campus = (new MpModel())->getMpLists($param);
            $this->success('success', '', $campus, 0);
        }

        return $this->fetch();
    }

	/**
	 * 添加媒体
	 * User: wufeng
	 * Date: 2020/04/15
	 *
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
    public function add()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
			if($data['type'] == '请选择类型'){
				$this->error('请选择类型', null, '', 1);
			}
			if($data['type'] == 1){
				if($data['dy_appid'] && !$data['dy_secret']){
					$this->error('请填写订阅号密钥', null, '', 1);
				}else if(!$data['dy_appid'] && $data['dy_secret']){
					$this->error('请填写订阅号', null, '', 1);
				}else if(!$data['dy_appid'] && !$data['dy_secret']){
					$this->error('请填写订阅号、订阅号密钥', null, '', 1);
				}
				$data['fw_appid'] = $data['fw_secret'] = $data['app_secret'] = '-';
				unset($data['type']);
			}else if($data['type'] == 2){
				if($data['fw_appid'] && !$data['fw_secret']){
					$this->error('请填写服务号密钥', null, '', 1);
				}else if(!$data['fw_appid'] && $data['fw_secret']){
					$this->error('请填写服务号', null, '', 1);
				}else if(!$data['fw_appid'] && !$data['fw_secret']){
					$this->error('请填写服务号、服务号密钥', null, '', 1);
				}
				$data['dy_appid'] = $data['dy_secret'] = $data['app_secret'] = '-';
				unset($data['type']);
			}else if($data['type'] == 3){
				if(!$data['app_secret']){
					$this->error('请填写app密钥', null, '', 1);
				}
				$data['dy_appid'] = $data['dy_secret'] = $data['fw_appid'] = $data['fw_secret'] = '-';
				unset($data['type']);
			}
			//验证媒体名称是否存在
			$where = array(
				'mp_name' => $data['mp_name'],
				'com_name' => $data['com_name'],
			);
			$checkMp = (new MpModel())->get($where);
			if($checkMp){
				//如果存在并且是已删除的状态  则回复正常状态
				if ($checkMp['status'] == -1) {
					(new MpModel())->where($where)->update(['status' => 0]);
					$this->success('添加成功', '', '', 1);
				}
				$this->error('媒体已存在', null, '', 1);
			}

            $res = (new MpModel())->save($data);
            if (false !== $res) {
                $this->success('添加成功', '', '', 1);
            }
            $this->error('添加失败', null, '', 1);
        }

        return $this->fetch();
    }

	/**
	 * 编辑媒体
	 * User: wufeng
	 * Date: 2020/04/15
	 *
	 * @return mixed
	 * @throws \think\exception\DbException
	 */
    public function edit()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');

			if($data['type'] == 1){
				if($data['dy_appid'] && !$data['dy_secret']){
					$this->error('请填写订阅号密钥', null, '', 1);
				}else if(!$data['dy_appid'] && $data['dy_secret']){
					$this->error('请填写订阅号', null, '', 1);
				}else if(!$data['dy_appid'] && !$data['dy_secret']){
					$this->error('请填写订阅号、订阅号密钥', null, '', 1);
				}
			}else if($data['type'] == 2){
				if($data['fw_appid'] && !$data['fw_secret']){
					$this->error('请填写服务号密钥', null, '', 1);
				}else if(!$data['fw_appid'] && $data['fw_secret']){
					$this->error('请填写服务号', null, '', 1);
				}else if(!$data['fw_appid'] && !$data['fw_secret']){
					$this->error('请填写服务号、服务号密钥', null, '', 1);
				}
			}else if($data['type'] == 3){
				if(!$data['app_secret']){
					$this->error('请填写app密钥', null, '', 1);
				}
			}
			unset($data['type'],$data['current']);
			//验证媒体名称是否存在
			$where = array(
				'mp_name' => $data['mp_name'],
				'com_name' => $data['com_name'],
			);
			$checkMp = (new MpModel())->get($where);
			if($checkMp){
				if($checkMp['id'] != $data['id']){
					$this->error('媒体已存在', null, '', 1);
				}
			}
            $res = (new MpModel())->save($data, ['id' => $data['id']]);
            if (false !== $res) {
                $this->success('编辑成功', '', '', 1);
            }
            $this->error('添加失败', null, '', 1);
        }
        $mp = (new MpModel())->get(input('id'));
        $type = '';
        if ('-' !== $mp['dy_appid']) {
            $type = 1;
        } elseif ('-' !== $mp['fw_appid']) {
            $type = 2;
        } elseif ('-' !== $mp['app_secret']) {
            $type = 3;
        }

        return $this->fetch('', [
            'type' => $type,
            'mp' => $mp
        ]);
    }

    /**
     * 媒体状态
     * User: wufeng
     * Date: 2020/03/31.
     */
    public function mp_status()
    {
        if ($this->request->isAjax() && $this->request->isPost()) {
            $data = input('post.');
            if (1 === (int) $data['status']) {
                $msg = '启用';
            } else {
                $msg = '禁用';
            }

            $res = (new MpModel())->save($data, ['id' => $data['id']]);

            if (false !== $res) {
                $this->success($msg.'成功', '', '', 1);
            }
            $this->error($msg.'失败', null, '', 1);
        }
    }
}
